#include "widget.h"
#include "ui_widget.h"
#include <QShortcut>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    //设置快捷键，-为减少，=为增加，需要用到shortcut
    QShortcut* shortCut1 = new QShortcut(this);
    shortCut1->setKey(QKeySequence("-"));
    QShortcut* shortCut2 = new QShortcut(this);
    shortCut2->setKey(QKeySequence("="));

    //通过信号槽，来感知快捷键被按下
    connect(shortCut1, &QShortcut::activated, this, &Widget::subnum);
    connect(shortCut2, &QShortcut::activated, this, &Widget::addnum);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::subnum()
{
    int value = ui->horizontalSlider->value();
    if(value <= ui->horizontalSlider->minimum())
    {
        return;
    }
    ui->horizontalSlider->setValue(value - 5);
}

void Widget::addnum()
{
    int value = ui->horizontalSlider->value();
    if(value >= ui->horizontalSlider->maximum())
    {
        return;
    }
    ui->horizontalSlider->setValue(value + 5);
}

void Widget::on_horizontalSlider_valueChanged(int value)
{
    ui->label->setText("当前值为：" + QString::number(value));
}

